package com.qf.controller;

import com.qf.pojo.Jiudian;
import com.qf.service.JiudianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/jiudian")
public class JiudianController {
    @Autowired
    private JiudianService jiudianService;
    /*首页-------------------------------模糊*/
    //模糊查询酒店
    @RequestMapping("/selectshouyemohu/{jname}")
    @ResponseBody
    List<Jiudian> selectshouyemohu(String jname) {
        List<Jiudian> selectshouyemohu = jiudianService.selectshouyemohu(jname);
        return selectshouyemohu;
    }
/*酒店页面--------------------------*/
    //查询所有酒店
    @RequestMapping("/selectAll")
    @ResponseBody
    public List<Jiudian> selectAll() {
        return jiudianService.findAll();
    }

    //根据酒店ID查询酒店返回酒店
    @RequestMapping("/selectJiudianById/{id}")
    @ResponseBody
    public Jiudian selectJiudianById(@PathVariable Integer id) {
        Jiudian jiudian = jiudianService.selectJiudianById(id);
        return jiudian;
    }

    //根据酒店名字模糊查酒店以及酒店要有房间数（根据酒店名字查酒店id再和房型表关联，查所有房型的总数量）
    @RequestMapping("/selectJiudianByJname/{jname}")
    @ResponseBody
    public List<Jiudian> selectJiudianByJname(@PathVariable String jname) {
        List<Jiudian> jiudians = jiudianService.selectJiudianByJname(jname);
        return jiudians;
    }
/*后台操作---------------------------------------*/
    //查询所有酒店
        @RequestMapping("/selectAllJiudian")
        public String selectAllJiudian(Model model) {
            List<Jiudian> jiudianlists = jiudianService.findAll();
            model.addAttribute("jiudianlist", jiudianlists);
            return "jiudianlist";
    }
    //根据酒店id删除酒店，成功返回success页面
    @RequestMapping("/deleteJiudianByJid/{jid}")
    @ResponseBody
    public String deleteJiudianByJid(@PathVariable Integer jid) {
        jiudianService.deleteJiudianByJid(jid);
        return "success";
    }

    /*传酒店对象，添加酒店，返回到success页面*/
    @RequestMapping("/addJiudian")
    public String addJiudian(Jiudian jiudian) {
        jiudianService.addJiudian(jiudian);
        return "success";
    }
    //修改酒店，先显示，到修改页面
    @RequestMapping("/findJiudianByJid/{jid}")
    public String updateJiudianByJid(@PathVariable Integer jid,Model model) {
        Jiudian jiudian = jiudianService.selectJiudianById(jid);
        model.addAttribute("jiudian",jiudian);
        return "updatejiudian";/*跳到酒店修改页面*/
    }
    //    在修改页面修改后，提交到重定向到酒店列表
    @RequestMapping("/updateJiudian")
    public String updateJiudian(Jiudian jiudian){
        /*修改的方法*/
        jiudianService.updateJiudian(jiudian);
        return "redirect:/jiudian/selectAllJiudian";/*酒店的列表*/
    }
}
